package com.local.web.service.impl;

import com.local.common.constants.BusinessException;
import com.local.common.constants.ExceptionCodeEnum;
import com.local.common.util.PageResultUtil;
import com.local.common.util.ResultUtil;
import com.local.goods.feign.GoodsServiceFeign;
import com.local.goods.pojo.Goods;
import com.local.goods.pojo.GoodsESDoc;
import com.local.goods.pojo.GoodsESRequestParam;
import com.local.web.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsServiceFeign goodsServiceFeign;

    @Override
    public Goods getById(Integer id) {
        ResultUtil<Goods> goodsResult = goodsServiceFeign.getGoodsById(id);
        if (goodsResult.getCode() != 200) {
            throw new BusinessException(goodsResult.getCode(), goodsResult.getMessage());
        }
        Goods goods = goodsResult.getData();
        return goods;
    }

    @Override
    public ResultUtil<PageResultUtil<GoodsESDoc>> search(GoodsESRequestParam param) {
        ResultUtil search = goodsServiceFeign.search(param);
        return search;
    }


    /**
     * 搜索的自动补齐，即搜索建议
     * @param keywords
     * @return
     */
    public ResultUtil<List<String>> searchSuggestion(String keywords) {
        List<String> list = new ArrayList<>();
        ResultUtil<List<String>> listResult = goodsServiceFeign.searchSuggestion(keywords);
        if (Objects.isNull(listResult)) {
            return new ResultUtil<>(201, "请求失败，请稍后再试", null);
        }
        return listResult;
    }

}
